package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomerDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/customers")
public class CustomerController {
    @Autowired
    private CustomerService  customerService;
    @GetMapping
    public Result list(CustomerDto customerDto)
    {
        log.info("查询所有客户");
        PageResult<Customer> pageResult = customerService.listCustomers(customerDto);
        return Result.success(pageResult);
    }
    @Log
    @PostMapping
    public Result save(@RequestBody Customer customer)
    {
        log.info("保存客户");
        customer.setCreateTime(LocalDateTime.now());
        customerService.save(customer);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id)
        {
            log.info("查询客户");
            Customer customer = customerService.getById(id);
            return Result.success(customer);
        }
    @Log
        @PutMapping
    public Result update(@RequestBody Customer customer)
            {
            log.info("更新客户");
            customer.setUpdateTime(LocalDateTime.now());
            customerService.updateById(customer);
            return Result.success();
        }
}
